/*
 * Copyright (C) 2018 guomw.Inc. All rights reserved.
 * 项目名称： report-manage
 * 文件名称： SimpleConnectionProvider.java
 * Date：18-11-5 上午10:54
 * Author: guomw
 */

package com.guomw.report.service.service.upgrade;

import org.eclipse.persistence.platform.database.DatabasePlatform;
import org.eclipse.persistence.platform.database.MySQLPlatform;
import org.eclipse.persistence.platform.database.SQLServerPlatform;
import org.springframework.jdbc.support.JdbcUtils;

import java.sql.Connection;
import java.sql.SQLException;

/**
 * @author guomw
 * @date 2018/9/11
 */
public class SimpleConnectionProvider implements ConnectionProvider {
    private final Connection connection;
    private final DatabasePlatform profile;

    public SimpleConnectionProvider(Connection connection) throws SQLException {
        this.connection = connection;
        String databaseName = JdbcUtils.commonDatabaseName(connection.getMetaData().getDatabaseProductName());
        if (databaseName.equalsIgnoreCase("MySQL")) {
            profile = new MySQLPlatform();
        } else if (databaseName.equalsIgnoreCase("Microsoft SQL Server")) {
            profile = new SQLServerPlatform();
        } else {
            throw new InternalError("unsupported Database " + databaseName);
        }
    }

    @Override
    public Connection getConnection() {
        return connection;
    }

    @Override
    public DatabasePlatform profile() {
        return profile;
    }
}
